package com.woniu.mealsService.service.impl;





import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.dto.RemoveByIdSqlDTO;
import com.woniu.dto.SelectProductByStoreIdDTO;
import com.woniu.exception.meals.MealsException;
import com.woniu.mealsService.mapper.ProductMapper;
import com.woniu.mealsService.model.Product;
import com.woniu.mealsService.service.ProductService;
import com.woniu.param.PageParam;
import com.woniu.utils.EnumException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 菜品表 服务实现类
 * </p>
 *
 * @author 吕雨泽
 * @since 2024年12月06日
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Override
    public PageInfo<SelectProductByStoreIdDTO> searchProductByStoreId(String storeId, PageParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<SelectProductByStoreIdDTO> selectProductByStoreIdDTOS = productMapper.searchProductByStoreId(storeId);
        return new PageInfo<>(selectProductByStoreIdDTOS);
    }

    @Override
    public void removeProductById(String storeId, Long productId) throws MealsException {
        RemoveByIdSqlDTO removeByIdSqlDTO = new RemoveByIdSqlDTO();
        removeByIdSqlDTO.setStoreId(storeId);
        removeByIdSqlDTO.setProductId(productId);
        removeByIdSqlDTO.setUpdateTime(LocalDateTime.now().toString());
        Integer count = productMapper.removeProductById(removeByIdSqlDTO);
        if (count==0){
            throw new MealsException(EnumException.PRODUCT_DELETE_FAILURE);
        }
    }
}
